const rowsPerPage = 10;
const allTrs = document.querySelectorAll('tbody tr');
let currentPage = 1;

// 显示当前表格内容
function renderTable() {
    const start = (currentPage - 1) * rowsPerPage;
    const end = Math.min(start + rowsPerPage, allTrs.length);

    allTrs.forEach((tr, index) => {
        tr.style.display = (index >= start && index < end) ? 'table-row' : 'none';
    });
}

// 更新分页按钮状态
function updatePaginationButtons() {
    const buttons = document.querySelectorAll('.pagination button');
    const prePage = document.querySelector('.pre-page');
    const nextPage = document.querySelector('.next-page');
    const totalPages = Math.ceil(allTrs.length / rowsPerPage);

    buttons.forEach((button, index) => {
        if (index + 1 === currentPage) {
            button.classList.add('active');
        } else {
            button.classList.remove('active');
        }
    });

    // Previous button
    if (currentPage === 1) {
        prePage.classList.add('disabled');
        prePage.style.cursor = 'not-allowed';
    } else {
        prePage.classList.remove('disabled');
        prePage.style.cursor = 'pointer';
    }

    // Next button
    if (currentPage === totalPages) {
        nextPage.classList.add('disabled');
        nextPage.style.cursor = 'not-allowed';
    } else {
        nextPage.classList.remove('disabled');
        nextPage.style.cursor = 'pointer';
    }
}

// 跳转到指定页面
function goToPage(page) {
    if (page < 1 || page > Math.ceil(allTrs.length / rowsPerPage)) return;

    currentPage = page;

    // 渲染页面内容
    renderTable();

    // 更新分页按钮状态
    updatePaginationButtons();
}

// 点击上一页按钮
document.querySelector('.pre-page').addEventListener('click', () => {
    if (currentPage > 1) goToPage(currentPage - 1);
});

// 点击下一页按钮
document.querySelector('.next-page').addEventListener('click', () => {
    if (currentPage < Math.ceil(allTrs.length / rowsPerPage)) goToPage(currentPage + 1);
});

// 点击具体页面按钮
document.querySelectorAll('.pagination button').forEach((button, index) => {
    button.addEventListener('click', () => {
        if (index + 1 !== currentPage) {
            goToPage(index + 1);
        }
    });
});

// 初始化渲染和分页按钮
renderTable();
updatePaginationButtons();